package cn.kasa.org.controller;

import cn.kasa.basic.utils.AjaxResult;
import cn.kasa.basic.utils.PageList;
import cn.kasa.org.domain.Employee;
import cn.kasa.org.query.EmployeeQuery;
import cn.kasa.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "部门的API" , description="部门相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @GetMapping
    @ApiOperation(value = "查询全部" )
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }

    @GetMapping("/{id}")

    public Employee loadById(@PathVariable("id") Long id){
        return employeeService.loadById(id);
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }


    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Employee employee){
        try{
            if(employee.getId() == null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PostMapping
    public PageList<Employee> queryData(@RequestBody EmployeeQuery employeeQuery){
        PageList<Employee> employees = employeeService.queryData(employeeQuery);
        return employees;
    }

    @PatchMapping
    public AjaxResult batchEmployee(@RequestBody Long[] ids){
        try {
            employeeService.batchRemove(ids);
            return AjaxResult.me();
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @GetMapping("/tree")
    public List<Employee> tree(){
        return employeeService.tree();
    }

}
